package 复习.面试TOP101.链表;

public class 删除链表的第n个元素 {
    public ListNode removeNthFromEnd (ListNode head, int n) {
        // write code here
        ListNode node = new ListNode(-1);
        node.next = head;
        ListNode cur = head;
        while(n-- > 0){
            cur = cur.next;
        }
        ListNode pre = node;
        while(cur != null){
            pre = pre.next;
            cur = cur.next;
        }
        pre.next = pre.next.next;
        return node.next;
    }
}
/*
public ListNode removeNthFromEnd (ListNode head, int n) {
        // write code here
        ListNode node = new ListNode(-1);
        node.next = head;
        ListNode cur = head;
        while (n-- > 1){
            cur = cur.next;
        }
        ListNode pre = node;
        while(cur.next != null){
           pre = pre.next;
           cur = cur.next;
        }
        pre.next = pre.next.next;
        return node.next;
    }
 */